<?php
/**
 * Created by PhpStorm.
 * User: QM
 * Date: 2018/3/21
 * Time: 10:24
 */
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class IndexController
 * @package App\Controller
 */
class IndexController extends Controller
{
    /**
     * 商品列表
     * @Route("/",name="index")
     * @return Response
     */
    public function index()
    {
        //$user = $this->getUser();
        $result = $this->get('goods_model')->getGoodsList();
        if (!isset($result['code']) || $result['code'] != 0) {
            return new Response("查询失败");
        }

        return $this->render("buy/index.html.twig", array("goods" => $result['data']));
    }

    /**
     * 获取商品抢购详情页面
     * @Route("/goods/attr", name="goods_attr")
     * @param Request $request
     * @return Response
     */
    public function goodsAttr(Request $request)
    {
        $goodsId = $request->get('goodsId', '');

        $result = $this->get("goods_model")->getGoodsAttr($goodsId);
        if (!isset($result['code']) || $result['code'] != 0) {
            return new Response("查询失败");
        }

        return $this->render("buy/buy.html.twig", array('goods' => $result['data']));
    }

    /**
     * 抢购
     * @Route("snap/up", name="snap_up")
     * @param Request $request
     * @return JsonResponse
     */
    public function snapUp(Request $request)
    {
        $goodsId = $request->get('goods_id', 0);
        $user = $this->getUser();

        $result = $this->get("goods_model")->snapUp($user, $goodsId);

        return new JsonResponse($result);
    }

    /**
     * 导出
     * @Route("export", name="export")
     * @param Request $request
     * @return Response
     */
    public function export(Request $request)
    {
        $goodsId = $request->get('goodsId', 0);
        $user = $this->getUser();
        $result = $this->get("goods_model")->createExport($user);
        if (!isset($result['code']) || $result['code'] != 0) {
            return new Response($result['message']);
        }

        return new Response($result['message']);
    }
}